#include <iostream>
#include <string>
#include "String.h"

using namespace std;


int func(int &, int, int*);
string func(string, string);
String func(String, String);
void ptrRef(int *& pr1, int *&pr2);
void printArray(int (&arr)[5]);
void printRefArr(int arr[5]);

int &get(int *arry, int index) { return arry[index]; }


void functionParameters()
{
	int iaa[10];
	for (int i = 0; i != 10; ++i)
		get(iaa, i) = 0; // change the value of all elements in the array!!

	int ia = 2, ib = 5, ic = 0;
	int *ip = &ic;
	string sa = "abc", sb = "1234";
	String SA("abc"), SB("1234");
	
	int id = func(ia, ib,ip);
	string sc = func(sa, sb);
	String SC = func(SA, SB);

	int *ip2 = &ib;
	int *&pr1 = ip, *&pr2 = ip2;
	ptrRef(pr1, pr2);

	/* Array as formal parameters */
	int i = 0, j[2] = {0, 1};
	int k[5] = {0, 2, 4, 6, 8};
	// printArray(&i); // IntelliSense: initial value of reference to non-const must be an lvalue; Error C2664: 'printArray' : cannot convert parameter 1 from 'int *' to 'int (&)[5]'
	printRefArr(&i); // complied well, will cause runtime error
	// printArray(j); // IntelliSense: a reference of type "int (&)[5]" (not const-qualified) cannot be initialized with a value of type "int [2]"; Error C2664: 'printArray' : cannot convert parameter 1 from 'int [2]' to 'int (&)[5]'
	printRefArr(j); // // complied well, will cause runtime error
	printArray(k);
	printRefArr(k);

	getchar();
}

int func(int &a, int b, int *c)
{
	*c  = 100;
	int ia = 111;

	c = &ia;
	if(a > b)
		return ++a;
	else
		return ++b;
}

string func(string strA, string strB)
{
	if(strA.size() > strB.size())
	{
		strA += ": longer";
		return strA;
	}
	else
	{
		strB += ": longer";
		return strB;
	}
}

String func(String strA, String strB)
{
	if(strA.size() > strB.size())
	{
		strA = "A: longer";
		return strA;
	}
	else
	{
		strB = "B: longer";
		return strB;
	}
}

void ptrRef(int *& pr1, int *&pr2)
{
	int *tmp = pr2;
	pr2 = pr1;
	pr1 = tmp;
}

void printArray(int (&arr)[5])
{
	for(int i = 0; i < 5; i ++)
	{
		cout << i + 1 << ": " << arr[i] << endl;
	}
}

void printRefArr(int arr[5])
{
	for(int i = 0; i < 5; i ++)
	{
		cout << i + 1 << ": " << arr[i] << endl;
	}
}